package cn.xiaoliu.basic.service.impl;

import cn.xiaoliu.basic.service.IBaseService;
import cn.xiaoliu.basic.mapper.BaseMapper;
import cn.xiaoliu.basic.query.BaseQuery;
import cn.xiaoliu.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

// @Servive  这里面不能加service，因为这个不能交给容器,只能交给真正的业务实现类去继承
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T>{

    //T  泛型一定要加，在具体的业务实现类中，再来声明t是哪一个类型
    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void add(T t) {
        mapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void remove(Long id) {
        mapper.delete(id);
    }

    @Override
    public T queryById(Long id) {
        return mapper.loadById(id);
    }

    @Override
    public List<T> queryAll() {
        return mapper.loadAll();
    }

    @Override
    @Transactional
    public PageList<T> queryPage(BaseQuery query) {
        //先查询总数
        Long total = mapper.queryCount(query);
        //再查询分页数据
        List<T> rows = mapper.queryData(query);
        return new PageList(total,rows);
    }

    @Override
    @Transactional
    public void pachRemove(List<Long> ids) {
        mapper.pachRemove(ids);
    }
}
